package com.fly.demo.auto;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 
 * 刷新配置文件
 * 
 * @author 00fly
 * @version [版本号, 2018年7月20日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Component
public class ReloadConfig
{
    private static final Logger logger = LoggerFactory.getLogger(ReloadConfig.class);
    
    private PropertiesConfiguration config;
    
    public ReloadConfig()
    {
        super();
        try
        {
            config = new PropertiesConfiguration("redis.properties");
            FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
            strategy.setRefreshDelay(5000L);// 刷新周期5s
            config.setReloadingStrategy(strategy);
        }
        catch (ConfigurationException e)
        {
            logger.error("刷新配置数据出错", e);
        }
    }
    
    /**
     * 刷新配置数据到对象中
     * 
     * @param redisConfig
     * @see [类、类#方法、类#成员]
     */
    public void refresh(RedisConfig redisConfig)
    {
        redisConfig.setPort(config.getInt("port"));
    }
}